package com.springboot.controller.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.mapper.OrderFormMapper;
import com.springboot.mapper.PatientInfoFormMapper;
import com.springboot.mapper.UserLoginFormMapper;
import com.springboot.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
@Controller
public class AppointmentInfoGetController {
    @Autowired
    OrderFormMapper orderFormMapper;
    @Autowired
    PatientInfoFormMapper patientInfoFormMapper;
    @Autowired
    UserLoginFormMapper userLoginFormMapper;

    @RequestMapping("/appointment_management")
    @ResponseBody
    public PageInfo getPatient(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        //先去orderform表中查询预约id，患者id，医生id，预约日期，就诊状态
        //再去查询患者姓名，电话号，医生姓名，病情图片
        PageHelper.startPage(pageNum,pageSize);
//        ArrayList<OrderForm> orderForms = orderFormMapper.selectAllOrderForm();
        ArrayList<AppointmentMageInfo> appointmentMageInfos = orderFormMapper.selectAllOrderForm();
        if(appointmentMageInfos!=null&&appointmentMageInfos.size()>0){
            for (AppointmentMageInfo appointmentMageInfo :appointmentMageInfos) {
                UserLoginForm patient = userLoginFormMapper.selectByPrimaryKey(appointmentMageInfo.getPatientId());
                UserLoginForm doctor = userLoginFormMapper.selectByPrimaryKey(appointmentMageInfo.getDoctorId());
                PatientInfoForm patientInfoForm = patientInfoFormMapper.selectByPrimaryKey(appointmentMageInfo.getBootId());

                appointmentMageInfo.setName(patient.getUserName());
                appointmentMageInfo.setTelephone(patient.getUserTel());
                appointmentMageInfo.setDoctorName(doctor.getUserName());
                appointmentMageInfo.setInfo(patientInfoForm.getSickInfo());
            }

            return PageInfo.of(appointmentMageInfos);
        }else {
            return PageInfo.of(appointmentMageInfos);
        }

    }

    @RequestMapping("/appointment_management/{queryString}")
    @ResponseBody
    public PageInfo getQuerystringPatient(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @PathVariable String queryString){
        String nameRegex = "[a-zA-Z\\u4e00-\\u9fa5 ]+";
            if(queryString.contains("就诊")){  //查询关键词是已就诊或未就诊
                PageHelper.startPage(pageNum,pageSize);
                ArrayList<AppointmentMageInfo> appointmentMageInfos = orderFormMapper.selectVisitOrderForm(queryString);
                if(appointmentMageInfos!=null&&appointmentMageInfos.size()>0){
                    for (AppointmentMageInfo appointmentMageInfo :appointmentMageInfos) {
                        UserLoginForm patient = userLoginFormMapper.selectByPrimaryKey(appointmentMageInfo.getPatientId());
                        UserLoginForm doctor = userLoginFormMapper.selectByPrimaryKey(appointmentMageInfo.getDoctorId());
                        PatientInfoForm patientInfoForm = patientInfoFormMapper.selectByPrimaryKey(appointmentMageInfo.getBootId());

                        appointmentMageInfo.setName(patient.getUserName());
                        appointmentMageInfo.setTelephone(patient.getUserTel());
                        appointmentMageInfo.setDoctorName(doctor.getUserName());
                        appointmentMageInfo.setInfo(patientInfoForm.getSickInfo());
                    }
                    return PageInfo.of(appointmentMageInfos);
                }else {
                    return PageInfo.of(appointmentMageInfos);
                }
            }
            else if(queryString.matches(nameRegex)){  //查询关键词是患者姓名
                UserLoginForm userLoginForm = userLoginFormMapper.selectUserByName(queryString);
                PageHelper.startPage(pageNum,pageSize);
                ArrayList<AppointmentMageInfo> appointmentMageInfos = orderFormMapper.selectById(userLoginForm.getUserId());
                if(appointmentMageInfos!=null&&appointmentMageInfos.size()>0){
                    for (AppointmentMageInfo appointmentMageInfo :appointmentMageInfos) {
                        UserLoginForm patient = userLoginFormMapper.selectByPrimaryKey(appointmentMageInfo.getPatientId());
                        UserLoginForm doctor = userLoginFormMapper.selectByPrimaryKey(appointmentMageInfo.getDoctorId());
                        PatientInfoForm patientInfoForm = patientInfoFormMapper.selectByPrimaryKey(appointmentMageInfo.getBootId());

                        appointmentMageInfo.setName(patient.getUserName());
                        appointmentMageInfo.setTelephone(patient.getUserTel());
                        appointmentMageInfo.setDoctorName(doctor.getUserName());
                        appointmentMageInfo.setInfo(patientInfoForm.getSickInfo());
                    }
                    return PageInfo.of(appointmentMageInfos);
                }else {
                    return PageInfo.of(appointmentMageInfos);
                }
            }
            else {  //其他关键词
                return PageInfo.of(new ArrayList<>());
            }

    }

    @RequestMapping("/deleteOrderForm")
    @ResponseBody
    public String deleteOrderForm(@RequestParam Integer bootId){
        int i = orderFormMapper.deleteByPrimaryKey(bootId);
        if(i>0)return "success";
        else return "fail";
    }
}
